//
//  RTJForgetPassWordController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/12/1.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJForgetPassWordController.h"
#import "UIButton+Category.h"

@interface RTJForgetPassWordController ()<UITextFieldDelegate,ZXRegTextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *sendSMSBtn;
@property (weak, nonatomic) IBOutlet UITextField *codeTxt;
@property (weak, nonatomic) IBOutlet ZXRegTextField *pwTet;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (weak, nonatomic) IBOutlet UILabel *phoneLab;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewtop;

@property (nonatomic,assign) BOOL isShow;
@property (weak, nonatomic) IBOutlet UILabel *titleLab;

@end

@implementation RTJForgetPassWordController

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.viewtop.constant = screenH == 812 ? 110 : 90; self.navigationController.navigationBarHidden = NO;
    
    _titleLab.hidden = _isForgetPassWord ? NO : YES;
    _codeTxt.delegate = self;
    _pwTet.delegate = self;
    _pwTet.zxdelegate = self;
    _pwTet.secureTextEntry = YES;
    [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    
    if (_isPayPassWord) {
        
        NSString *phone = [NSString stringWithFormat:@"%ld",(long)[RTJUserTool getTheUserModel].mobile];
        _phoneLab.text = [Apputil replaceStringWithAsterisk:phone startLocation:3 lenght:phone.length-7];
        
//          [self autoSendSMS:@"user/get_pay_password_code" dic:nil];
        
    }else{
        
        //默认为0表示修改登录密码，会做登录状态验证，1表示忘记密码不会验证登录状态
        
        
        //忘记密码
        if (_isForgetPassWord) {
            
            _phoneLab.text = [Apputil replaceStringWithAsterisk:_phoneStr startLocation:3 lenght:_phoneStr.length-7];

//            [self autoSendSMS:@"user/get_password_code" dic:@{@"type":@"1",
//                                                              @"mobile":_phoneStr
//                                                              }];
        }else{
            
            NSString *psd = [NSString stringWithFormat:@"%ld",(long)[RTJUserTool getTheUserModel].mobile];
            _phoneLab.text = [Apputil replaceStringWithAsterisk:psd startLocation:3 lenght:psd.length-7];

            //修改密码
//            [self autoSendSMS:@"user/get_password_code" dic:@{@"type":@"0"}];
        }
    }
    
    self.sendSMSBtn.layer.borderColor = [UIColor colorForHex:@"#e5e5e5"].CGColor;
    self.sendSMSBtn.layer.borderWidth = 1;
    self.sendSMSBtn.layer.cornerRadius = 5;
    self.sendSMSBtn.layer.masksToBounds = YES;
    
}

-(void)autoSendSMS:(NSString *)url dic:(NSDictionary *)dic{
    
    [RTJNetWork asyncRequestWithURL:url params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                [SVProgressHUD showSuccessWithStatus:@"验证短信已经发送，请注意查收"];
                
                [self.sendSMSBtn startWithTime: 60 title:@"重新获取验证码" countDownTitle:@"s" mainColor:[UIColor whiteColor] countColor:[UIColor whiteColor]];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"短信发送失败:%@",data[@"msg"]]];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"短信发送失败:%@",errorMsg]];

        }
    }];
}

-(void)updatePassWord:(NSString *)url dic:(NSDictionary *)dic{
    
    [RTJNetWork asyncRequestWithURL:url params:dic method:POST zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        if (success) {
            
            if ([data[@"status"]integerValue]==1) {
                
                [SVProgressHUD dismiss];
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];

        }
        
    }];
}


- (IBAction)sendSMS:(UIButton *)sender {
    
    if (_isPayPassWord) {
        
        [self autoSendSMS:@"user/get_pay_password_code" dic:nil];
        
    }else{
        //默认为0表示修改登录密码，会做登录状态验证，1表示忘记密码不会验证登录状态
        //忘记密码
        if (_isForgetPassWord) {
            
            [self autoSendSMS:@"user/get_password_code" dic:@{@"type":@"1",
                                                              @"mobile":_phoneStr
                                                              }];
        }else{
            
            //修改密码
            [self autoSendSMS:@"user/get_password_code" dic:@{@"type":@"0"}];
        }
    }
}


- (IBAction)showPassWord:(UIButton *)sender {
    
    sender.selected = !sender.selected;
    _isShow = sender.selected;
    _pwTet.secureTextEntry = !sender.selected;

}

- (IBAction)finish:(id)sender {
    
    if (_codeTxt.text.length == 0) {
        
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    
    if (_pwTet.text.length<6||_pwTet.text.length>20) {
        
        [SVProgressHUD showErrorWithStatus:@"请设置6-20位的密码"];
        return;
    }
    
    [SVProgressHUD showWithStatus:nil];
    
    if (_isPayPassWord) {
        
    [self updatePassWord:@"user/update_pay_password" dic:@{@"pay_password":_pwTet.text,
                                                           @"verify_code":_codeTxt.text}];
        
    }else{
                
        //忘记密码
        if (_isForgetPassWord) {
            
            [self updatePassWord:@"user/update_password" dic:@{@"type":@"1",
                                                                 @"user_pwd":_pwTet.text,
                                                                 @"verify_code":_codeTxt.text,
                                                           @"mobile":_phoneStr
                                                              }];
        }else{
            
            //修改密码
            [self updatePassWord:@"user/update_password" dic:@{@"type":@"0",
                                                                 @"verify_code":_codeTxt.text,@"user_pwd":_pwTet.text}];
        }
    }
    
    
    
    
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (_codeTxt.text.length != 0 || (_pwTet.text.length>5&&_pwTet.text.length<13)) {
        
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_ljcj"] forState: UIControlStateNormal];
        
    }else{
        
        [_finishBtn setBackgroundImage:[UIImage imageNamed:@"btn_qrcjhui"] forState: UIControlStateNormal];
    }
    
}



@end
